package vn.edu.uit.service.fetchers;

import java.util.ArrayList;

import org.json.JSONObject;

import vn.edu.uit.hotelbooking.JsonParse.DistrictParser;
import vn.edu.uit.hotelbooking.model.District;
import vn.edu.uit.service.UITAPIContant;
import vn.edu.uit.service.UITHttpRequest;
import vn.edu.uit.service.UITHttpRequest.RequestMethod;
import vn.edu.uit.service.UITService;
import android.util.Log;

public class DistrictFetcher {
    private static final String LOG_TAG = "DistrictFetcher";
    private static UITHttpRequest httpRequest;
    
    /**
     * The method to get the district list
     * @return If success, return a district arraylist. If fail, return null.
     */
    public static ArrayList<District> getDistrict(String provinceId) {
        UITService.REQUEST_SUCCESS = false;
        httpRequest = new UITHttpRequest(UITAPIContant.API_REST_DOMAIN_URL + UITAPIContant.Api_Functions.GET_DISTRICT);
        httpRequest.addParam(UITService.TAG_AUTHENTICATE, UITService.TOKEN);
        httpRequest.addParam(UITService.TAG_PROVINCEID, provinceId);
        try {
            JSONObject object = httpRequest.doExecute(RequestMethod.POST);
            Log.d("JSON", object.toString());
            if(object.getInt(UITService.TAG_ERROR)==0) {        //request was successful, return a district arraylist
                UITService.REQUEST_SUCCESS = true;
                return DistrictParser.getDistrictFromJson(object.getJSONArray(UITService.TAG_DATA));              
            } else {                                 //request was failed, return null
                UITService.REQUEST_SUCCESS = false;
                UITService.MSG = object.getString(UITService.TAG_MSG);
                return null;
            }   
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(LOG_TAG, e.getMessage());
        }
        return null;
    }
}
